package cn.edu.sjtu.se.dslab.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import cn.edu.sjtu.se.dslab.action.bean.PageBean;
import cn.edu.sjtu.se.dslab.component.data.DomainEntity;
import cn.edu.sjtu.se.dslab.dao.IBaseDao;
import cn.edu.sjtu.se.dslab.dao.IDomainEntityDao;
import cn.edu.sjtu.se.dslab.exception.DBException;
import cn.edu.sjtu.se.dslab.util.Const;
import cn.edu.sjtu.se.dslab.util.MD5;

@Repository("domainEntityDao")
public class DomainEntityDaoImpl implements IDomainEntityDao {

	@Resource(name = "baseDao")
	private IBaseDao<DomainEntity> baseDao;

	private static final String URI = "uri";
	
	private static final String DELETED = "deleted";

	@Override
	public void save(DomainEntity entity) {
		// TODO Auto-generated method stub
		String temp = entity.getName() 
				+ "data_entity" 
				+  System.currentTimeMillis();
		MD5 md5 = new MD5();
		String uri = md5.getMD5ofStr(temp).substring(0, 8);
		while (!checkUri(Const.URI_DATA_ENTITY+ ":" + uri)) {
			temp = entity.getName() 
					+ "data_entity" 
					+  System.currentTimeMillis();
			uri = md5.getMD5ofStr(temp).substring(0, 8);
		}
		entity.setUri(Const.URI_DATA_ENTITY+ ":" + uri);
		baseDao.save(entity);
	}

	@Override
	public void update(DomainEntity entity) {
		// TODO Auto-generated method stub
		baseDao.update(entity);
	}

	@Override
	public DomainEntity queryById(long id) {
		// TODO Auto-generated method stub
		return baseDao.queryById(DomainEntity.class, id);
	}

	@Override
	public DomainEntity queryByUri(String uri) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from DomainEntity where ");
		hql.append(URI + "=");
		hql.append("'" + uri + "' and ");
		hql.append(DELETED + "=0");
		return baseDao.queryObject(hql.toString());
	}

	@Override
	public boolean checkUri(String uri) {
		// TODO Auto-generated method stub
		DomainEntity entity = queryByUri(uri);
		if (entity != null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public PageBean<DomainEntity> queryByPageNum(int pageNo) throws DBException {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from DomainEntity where ");
		hql.append(DELETED + "=0");
		PageBean<DomainEntity> bean = baseDao.queryByspecialHqlForPage(pageNo,
				Const.PAGESIZE, hql.toString());
		return bean;
	}

	@Override
	public List<DomainEntity> queryAll() {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from DomainEntity d where ");
		hql.append("d.type='");
		hql.append(Const.DATA_ENTITY + "' and ");
		hql.append(DELETED + "=0");
		return baseDao.query(hql.toString());
	}

	@Override
	public PageBean<DomainEntity> queryDataEntityByPageNum(int pageNo)
			throws DBException {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from DomainEntity d where ");
		hql.append("d.type='");
		hql.append(Const.DATA_ENTITY + "' and ");
		hql.append(DELETED + "=0");
		PageBean<DomainEntity> bean = baseDao.queryByspecialHqlForPage(pageNo,
				Const.PAGESIZE,hql.toString());
		return bean;
	}

	@Override
	public List<DomainEntity> queryByProperty(String text) {
		// TODO Auto-generated method stub
			StringBuffer hql = new StringBuffer();
			// 拼接sql语句
			hql.append("from DomainEntity a where ");
			hql.append(" a.name like '%");
			hql.append( text+ "%' or ");
			hql.append(" a.cnName like '%");
			hql.append( text+ "%' or ");
			hql.append(" a.description like '%");
			hql.append( text+ "%' and a.type='数据实体' and ");
			hql.append(DELETED + "=0");
			return baseDao.query(hql.toString());
	}

	@Override
	public DomainEntity queryByName(String name) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from DomainEntity a where ");
		hql.append(" a.name = '");
		hql.append( name+ "' and ");
		hql.append(DELETED + "=0");
		return baseDao.queryObject(hql.toString());
	}

}
